﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Elasticsearch.Synchronize.Domain.Events;
using Elasticsearch.Synchronize.Domain.Interface;

namespace Elasticsearch.Synchronize.DomainEventHandler.Handler
{
    public class EnableRuleEventHandler : MediatR.NotificationHandler<EnableRuleDomainEvent>
    {
        private readonly ISynchronizeRuleRepository _repository;

        private readonly ISynchronizeItemRepository _itemRepository;

        public EnableRuleEventHandler(ISynchronizeRuleRepository repository, ISynchronizeItemRepository itemRepository)
        {
            _repository = repository;
            _itemRepository = itemRepository;
        }

        protected override void HandleCore(EnableRuleDomainEvent notification)
        {
            var rule = notification.SynchronizeRule;

            rule.Enabled = true;
            _repository.Update(rule.Identity, rule);

            _itemRepository.BatchEnable(rule.Identity);
        }
    }
}
